Rectangle area

Time: O(1); Space: O(1); easy

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Example 1:

Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2

Output: 45

Note:

  • Assume that the total area is never beyond the maximum possible value of int.

[1]:
class Solution1(object):
    def computeArea(self, A, B, C, D, E, F, G, H) -> int:
        """
        :type A: int
        :type B: int
        :type C: int
        :type D: int
        :type E: int
        :type F: int
        :type G: int
        :type H: int
        :rtype: int
        """
        return (D - B) * (C - A) + \
               (G - E) * (H - F) - \
               max(0, (min(C, G) - max(A, E))) * \
               max(0, (min(D, H) - max(B, F)))
[4]:
s = Solution1()
A, B, C, D = -3, 0, 3, 4
E, F, G, H = 0, -1, 9, 2
assert s.computeArea(A, B, C, D, E, F, G, H) == 45